package com.qdu.servlet;

import com.qdu.dao.UsersDao;
import com.qdu.dao.impl.ManagementDaoImpl;
import com.qdu.entity.Activity;
import com.qdu.entity.Log;
import com.qdu.entity.Management;
import com.qdu.entity.Users;
import com.qdu.service.ActivityService;
import com.qdu.service.impl.LogServiceImpl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 点击上方的链接之后如果管理员已经登陆了，那就直接跳到manager页面，否则就登录
 *
 * @author Lenovo
 */
@WebServlet("/wrmps")
public class whetherRegisterManagerPageServlet extends HttpServlet{

    ManagementDaoImpl mandao=new ManagementDaoImpl();
    LogServiceImpl logserviceimpl=new LogServiceImpl();
    ActivityService activityService=new ActivityService();
    UsersDao userdao=new UsersDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession(false);
        if(null==session)//如果连session都没有，那就一定要让他登录
        {
            resp.sendRedirect("registerManager.html");
        }
        else{
            String name=(String)session.getAttribute("name");
            List<Management> listM=mandao.selectAll();
            for(Management m:listM)
            {
                if(m.getName().equals(name))//有这个管理员
                {
                    List<Activity> activity=activityService.findAllActivity();
                    List<Users> users=userdao.getAllUsers();
                    List<Log> logList=logserviceimpl.getAllLog();
                    List<Users> list=new ArrayList<>();
                    for(Users u:users)//只显示通过审核的
                    {
                        if(u.getUstatus()==1)
                        {
                            list.add(u);
                        }
                    }
                    req.setAttribute("managerlist", activity);
                    req.setAttribute("logList", logList);//将所有日志放入属性
                    req.setAttribute("userList",list);
                    req.getRequestDispatcher("manager.jsp").forward(req, resp);
                    return;
                }
            }
            resp.sendRedirect("registerManager.html");
        }
    }

    
}
